/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package racer.client.gui.entities;

import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.KeyEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;

/**
 *
 * @author Piet
 */
public class CarEntity extends Entity {

    //private Game game;
    //private int carid;
    //private int angle;

    /*public CarEntity(Game game, int carid,int x, int y, BufferedImage carImage) {
        super(x, y,carid);
        this.game = game;
        this.carImage = carImage;
        this.angle=0;
    }*/
    
    public CarEntity(int carid, int x, int y, BufferedImage carImage) {
        super(x, y,carid);
        this.entImage = carImage;
        this.angle=0;
    }
/*
    @Override
    public void move(long delta) {
    	if(accelerating) {
    		x += Math.cos(Math.toRadians(angle))*speedX;
			x += Math.sin(Math.toRadians(angle))*speedY;
		}
    	if(braking) {
			x -= Math.cos(Math.toRadians(angle))*speedX;
			x -= Math.sin(Math.toRadians(angle))*speedY;
		}
    }*/

     /**
     * Beweegt de Entity horizontaal, rekening houdend met de grenzen van 
     * het scherm
     */
    @Override
    public void moveX(long delta) {
        if ((dx < 0) && (x < 10)) {
            return;
        }
        if ((dx > 0) && (x > 1014)) {
            return;
        }
        super.moveX(delta);
    }

    /**
     * Beweegt de Entity verticaal, rekening houdend met de grenzen van 
     * het scherm
     */
    @Override
    public void moveY(long delta) {
        if ((dy < 0) && (y < 10)) {
            return;
        }
        if ((dy > 0) && (y > 758)) {
            return;
        }
        super.moveY(delta);
    }

    public Point2D.Double getTopLeftCornerPosition() {
    	double x1, y1;
    	Point2D.Double p2d;
    	
    	x1 = x-entImage.getWidth()/2 - getCenterPosition().getX();
    	y1 = y-entImage.getHeight()/2 - getCenterPosition().getY();
    	p2d = rotateCoordinate(new Point2D.Double(x1,y1), angle);
    	return p2d;
    }    
    public Point2D.Double getTopRightCornerPosition() {
    	double x1, y1;
    	Point2D.Double p2d;
    	
    	x1 = x+entImage.getWidth()/2 - getCenterPosition().getX();
    	y1 = y-entImage.getHeight()/2 - getCenterPosition().getY();
    	p2d = rotateCoordinate(new Point2D.Double(x1,y1), angle);
    	return p2d;
    }    
    public Point2D.Double getBottomLeftCornerPosition() {
    	double x1, y1;
    	Point2D.Double p2d;
    	
    	x1 = x-entImage.getWidth()/2 - getCenterPosition().getX();
    	y1 = y+entImage.getHeight()/2 - getCenterPosition().getY();
    	p2d = rotateCoordinate(new Point2D.Double(x1,y1), angle);
    	return p2d;
    }    
    public Point2D.Double getBottomRightCornerPosition() {
    	double x1, y1;
    	Point2D.Double p2d;
    	
    	x1 = x+entImage.getWidth()/2 - getCenterPosition().getX();
    	y1 = y+entImage.getHeight()/2 - getCenterPosition().getY();
    	p2d = rotateCoordinate(new Point2D.Double(x1,y1), angle);
    	return p2d;
    }
    
    /**
     * according to following formulae (http://homepages.inf.ed.ac.uk/rbf/HIPR2/rotate.htm)
     * x2 = cos(a)*(x1-x0) - sin(a)*(y1-y0) + x0
     * y2 = sin(a)*(x1-x0) - cos(a)*(y1-y0) + y0
     */       
    private Point2D.Double rotateCoordinate(Point2D.Double p2d, double a) {
    	double xRot, yRot;
    	xRot = p2d.x*Math.cos(Math.toRadians(a)) - p2d.y*Math.sin(Math.toRadians(a)) + getCenterPosition().getX();
    	yRot = p2d.x*Math.sin(Math.toRadians(a)) + p2d.y*Math.cos(Math.toRadians(a)) + getCenterPosition().getY();
    	return new Point2D.Double(xRot,yRot);
    }

    public Point2D getMostTopPosition() {
    	Point2D.Double p2d = null;
    	if(angle >=0 && angle <= 90)
    		p2d = getTopLeftCornerPosition();
    	if(angle > 90 && angle <= 180)
    		p2d = getBottomLeftCornerPosition();
    	if(angle > 180 && angle <= 270)
    		p2d = getBottomRightCornerPosition();
    	if(angle > 270 && angle < 360)
    		p2d = getTopRightCornerPosition();
    	return p2d;
    }
    public Point2D getMostBottomPosition() {
    	Point2D.Double p2d = null;
    	if(angle >=0 && angle <= 90)
    		p2d = getBottomRightCornerPosition();
    	if(angle > 90 && angle <= 180)
    		p2d = getTopRightCornerPosition();
    	if(angle > 180 && angle <= 270)
    		p2d = getTopLeftCornerPosition();
    	if(angle > 270 && angle < 360)
    		p2d = getBottomLeftCornerPosition();
    	return p2d;
    }
    public Point2D getMostLeftPosition() {
    	Point2D.Double p2d = null;
    	if(angle >=0 && angle <= 90)
    		p2d = getBottomLeftCornerPosition();
    	if(angle > 90 && angle <= 180)
    		p2d = getBottomRightCornerPosition();
    	if(angle > 180 && angle <= 270)
    		p2d = getTopRightCornerPosition();
    	if(angle > 270 && angle < 360)
    		p2d = getTopLeftCornerPosition();
    	return p2d;
    }
    public Point2D getMostRightPosition() {
    	Point2D.Double p2d = null;
    	if(angle >=0 && angle <= 90)
    		p2d = getTopRightCornerPosition();
    	if(angle > 90 && angle <= 180)
    		p2d = getTopLeftCornerPosition();
    	if(angle > 180 && angle <= 270)
    		p2d = getBottomLeftCornerPosition();
    	if(angle > 270 && angle < 360)
    		p2d = getBottomRightCornerPosition();
    	return p2d;
    }
    
    public Point2D getCenterPosition() {
    	//Point2D.Double p2d = new Point2D.Double(x+entImage.getWidth()/2, y+entImage.getHeight()/2);
    	//return p2d;
    	return new Point2D.Double(x,y);
    }
    
    public Shape getShape() {
    	//Rectangle rect = new Rectangle((int)this.x, (int)this.y, entImage.getWidth(), entImage.getHeight());
    	Polygon rect = new Polygon();
    	rect.addPoint((int)this.getMostTopPosition().getX(), (int)this.getMostTopPosition().getY());
    	rect.addPoint((int)this.getMostRightPosition().getX(), (int)this.getMostRightPosition().getY());
    	rect.addPoint((int)this.getMostBottomPosition().getX(), (int)this.getMostBottomPosition().getY());
    	rect.addPoint((int)this.getMostLeftPosition().getX(), (int)this.getMostLeftPosition().getY());
    	
    	return rect;
    }
    
    public void setInitPosition(double x, double y, int angle) {
    	this.initX = x;
    	this.initY = y;
    	this.initAngle = angle;
    }
    public void setToInitPosition() {
    	this.x = this.initX;
    	this.y = this.initY;
    	this.angle = this.initAngle;
    }
    
}
